﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    public static class StaticFileHelper
    {
        /// <summary>
        /// 文件类型
        /// </summary>
        private enum FileType
        {
            Image = 0,
            JS,
            CSS,
            Html,
            Media,
            Other
        }

        /// <summary>
        /// 获取图片实际URL地址
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="imageName">图片名称(可能带相对路径)</param>
        /// <returns>图片实际路径</returns>
        public static string Image(this System.Web.Mvc.UrlHelper helper, string imageName)
        {
            return GetFullUrl(imageName, FileType.Image);
        }

        /// <summary>
        /// 获取JS实际URL路径
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="jsName">js文件名称</param>
        /// <returns></returns>
        public static string JS(this System.Web.Mvc.UrlHelper helper, string jsName)
        {
            return GetFullUrl(jsName, FileType.JS);
        }

        /// <summary>
        /// 获取Css样实际URL式路径
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="cssName">css路径(可能带相对路径)</param>
        /// <returns>css实际路径</returns>
        public static string CSS(this System.Web.Mvc.UrlHelper helper, string cssName)
        {
            return GetFullUrl(cssName, FileType.CSS);
        }

        /// <summary>
        /// 获取html实际URL路径
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="htmlName">html文件名称</param>
        /// <returns></returns>
        public static string Html(this System.Web.Mvc.UrlHelper helper, string htmlName)
        {
            return GetFullUrl(htmlName, FileType.JS);
        }

        /// <summary>
        /// 获取media实际URL路径
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="jsName">media文件名称</param>
        /// <returns></returns>
        public static string Media(this System.Web.Mvc.UrlHelper helper, string mediaName)
        {
            return GetFullUrl(mediaName, FileType.JS);
        }

        /// <summary>
        /// 根据文件名和文件类型获取文件实际路径
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileType">文件类型</param>
        /// <returns>文件实际路径</returns>
        private static string GetFullUrl(string fileName,FileType fileType)
        { 
            string url = System.Configuration.ConfigurationManager.AppSettings[fileType.ToString()];
            url = url.TrimEnd(new char[] { '/' }) + "/" + fileName.TrimStart(new char[] { '/','.' });
            return url;
        }
    }
}
